using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace F1RacingGame
{
    public class SettingsScreen : IGameScreen
    {
        private int selectedOption = 0;
        private string[] settingOptions = { "音效", "音乐", "自动演示", "难度", "返回" };
        private string[] difficultyOptions = { "简单", "普通", "困难" };
        private bool[] boolSettings = { true, true, true }; // 音效, 音乐, 自动演示
        private int difficultyIndex = 1; // 普通难度
        
        private Font titleFont;
        private Font menuFont;
        private Font selectedFont;
        private Font valueFont;
        private SolidBrush titleBrush;
        private SolidBrush menuBrush;
        private SolidBrush selectedBrush;
        private SolidBrush valueBrush;
        private SolidBrush backgroundBrush;
        
        public void Initialize()
        {
            titleFont = new Font("Arial", 32, FontStyle.Bold);
            menuFont = new Font("Arial", 18, FontStyle.Regular);
            selectedFont = new Font("Arial", 20, FontStyle.Bold);
            valueFont = new Font("Arial", 16, FontStyle.Regular);
            
            titleBrush = new SolidBrush(Color.FromArgb(255, 255, 215, 0)); // 金色
            menuBrush = new SolidBrush(Color.FromArgb(255, 255, 255, 255)); // 白色
            selectedBrush = new SolidBrush(Color.FromArgb(255, 255, 69, 0)); // 橙红色
            valueBrush = new SolidBrush(Color.FromArgb(255, 0, 255, 255)); // 青色
            backgroundBrush = new SolidBrush(Color.FromArgb(255, 0, 0, 50)); // 深蓝色
            
            // 加载当前设置
            LoadCurrentSettings();
        }
        
        private void LoadCurrentSettings()
        {
            boolSettings[0] = GameManager.Instance.SoundEnabled;
            boolSettings[1] = GameManager.Instance.MusicEnabled;
            boolSettings[2] = GameManager.Instance.DemoEnabled;
            difficultyIndex = (int)GameManager.Instance.Difficulty;
        }
        
        public void Update()
        {
            // 设置屏幕不需要特殊更新逻辑
        }
        
        public void Render(Graphics graphics)
        {
            // 确保资源已初始化
            if (backgroundBrush == null || titleFont == null || titleBrush == null)
            {
                Initialize();
            }
            
            // 绘制渐变背景
            DrawGradientBackground(graphics);
            
            // 绘制装饰性边框
            DrawDecorativeBorder(graphics);
            
            // 绘制标题
            string title = "游戏设置";
            SizeF titleSize = graphics.MeasureString(title, titleFont);
            float titleX = (GameEngine.GAME_WIDTH - titleSize.Width) / 2;
            
            // 绘制标题阴影
            SolidBrush shadowBrush = new SolidBrush(Color.FromArgb(100, 0, 0, 0));
            graphics.DrawString(title, titleFont, shadowBrush, titleX + 2, 82);
            shadowBrush.Dispose();
            
            graphics.DrawString(title, titleFont, titleBrush, titleX, 80);
            
            // 绘制设置选项
            float menuY = 200;
            for (int i = 0; i < settingOptions.Length; i++)
            {
                Font currentFont = (i == selectedOption) ? selectedFont : menuFont;
                SolidBrush currentBrush = (i == selectedOption) ? selectedBrush : menuBrush;
                
                string optionText = settingOptions[i];
                SizeF optionSize = graphics.MeasureString(optionText, currentFont);
                float optionX = 150;
                
                // 绘制选中项的装饰
                if (i == selectedOption)
                {
                    DrawSelectionIndicator(graphics, optionX - 20, menuY, optionSize.Width + 40);
                }
                
                graphics.DrawString(optionText, currentFont, currentBrush, optionX, menuY);
                
                // 绘制设置值
                string valueText = GetSettingValue(i);
                SizeF valueSize = graphics.MeasureString(valueText, valueFont);
                float valueX = 450;
                
                graphics.DrawString(valueText, valueFont, valueBrush, valueX, menuY + 2);
                
                menuY += 60;
            }
            
            // 绘制提示信息
            string hint = "使用方向键选择，回车键修改，ESC键返回";
            Font hintFont = new Font("Arial", 12, FontStyle.Regular);
            SolidBrush hintBrush = new SolidBrush(Color.FromArgb(180, 255, 255, 255));
            SizeF hintSize = graphics.MeasureString(hint, hintFont);
            float hintX = (GameEngine.GAME_WIDTH - hintSize.Width) / 2;
            graphics.DrawString(hint, hintFont, hintBrush, hintX, GameEngine.GAME_HEIGHT - 60);
            
            hintFont.Dispose();
            hintBrush.Dispose();
        }
        
        private void DrawGradientBackground(Graphics graphics)
        {
            // 创建渐变背景
            Rectangle rect = new Rectangle(0, 0, GameEngine.GAME_WIDTH, GameEngine.GAME_HEIGHT);
            using (LinearGradientBrush gradientBrush = new LinearGradientBrush(
                rect, 
                Color.FromArgb(255, 0, 0, 80), 
                Color.FromArgb(255, 0, 0, 30), 
                90f))
            {
                graphics.FillRectangle(gradientBrush, rect);
            }
        }
        
        private void DrawDecorativeBorder(Graphics graphics)
        {
            // 绘制装饰性边框
            Pen borderPen = new Pen(Color.FromArgb(150, 255, 215, 0), 3);
            graphics.DrawRectangle(borderPen, 20, 20, GameEngine.GAME_WIDTH - 40, GameEngine.GAME_HEIGHT - 40);
            borderPen.Dispose();
        }
        
        private void DrawSelectionIndicator(Graphics graphics, float x, float y, float width)
        {
            // 绘制选中项的装饰性指示器
            Pen indicatorPen = new Pen(Color.FromArgb(200, 255, 69, 0), 2);
            graphics.DrawRectangle(indicatorPen, x, y - 5, width, 35);
            
            // 绘制箭头
            Point[] leftArrow = {
                new Point((int)x - 8, (int)y + 12),
                new Point((int)x - 3, (int)y + 7),
                new Point((int)x - 3, (int)y + 17)
            };
            Point[] rightArrow = {
                new Point((int)(x + width + 8), (int)y + 12),
                new Point((int)(x + width + 3), (int)y + 7),
                new Point((int)(x + width + 3), (int)y + 17)
            };
            
            graphics.FillPolygon(new SolidBrush(Color.FromArgb(200, 255, 69, 0)), leftArrow);
            graphics.FillPolygon(new SolidBrush(Color.FromArgb(200, 255, 69, 0)), rightArrow);
            
            indicatorPen.Dispose();
        }
        
        private string GetSettingValue(int optionIndex)
        {
            switch (optionIndex)
            {
                case 0: // 音效
                    return boolSettings[0] ? "开启" : "关闭";
                case 1: // 音乐
                    return boolSettings[1] ? "开启" : "关闭";
                case 2: // 自动演示
                    return boolSettings[2] ? "开启" : "关闭";
                case 3: // 难度
                    return difficultyOptions[difficultyIndex];
                case 4: // 返回
                    return "";
                default:
                    return "";
            }
        }
        
        public void OnKeyDown(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                    selectedOption = (selectedOption - 1 + settingOptions.Length) % settingOptions.Length;
                    break;
                case Keys.Down:
                    selectedOption = (selectedOption + 1) % settingOptions.Length;
                    break;
                case Keys.Enter:
                    HandleSettingChange();
                    break;
                case Keys.Left:
                case Keys.Right:
                    if (selectedOption < 4) // 不是"返回"选项
                    {
                        HandleSettingChange();
                    }
                    break;
                case Keys.Escape:
                    SaveSettings();
                    GameManager.Instance.ChangeState(GameState.MainMenu);
                    break;
            }
        }
        
        public void OnKeyUp(KeyEventArgs e)
        {
            // 设置屏幕不需要处理按键释放
        }
        
        private void HandleSettingChange()
        {
            switch (selectedOption)
            {
                case 0: // 音效
                    boolSettings[0] = !boolSettings[0];
                    break;
                case 1: // 音乐
                    boolSettings[1] = !boolSettings[1];
                    break;
                case 2: // 自动演示
                    boolSettings[2] = !boolSettings[2];
                    break;
                case 3: // 难度
                    difficultyIndex = (difficultyIndex + 1) % difficultyOptions.Length;
                    break;
                case 4: // 返回
                    SaveSettings();
                    GameManager.Instance.ChangeState(GameState.MainMenu);
                    break;
            }
        }
        
        private void SaveSettings()
        {
            GameManager.Instance.SoundEnabled = boolSettings[0];
            GameManager.Instance.MusicEnabled = boolSettings[1];
            GameManager.Instance.DemoEnabled = boolSettings[2];
            GameManager.Instance.Difficulty = (GameDifficulty)difficultyIndex;
            GameManager.Instance.SaveGameSettings();
        }
        
        public void Dispose()
        {
            titleFont?.Dispose();
            menuFont?.Dispose();
            selectedFont?.Dispose();
            valueFont?.Dispose();
            titleBrush?.Dispose();
            menuBrush?.Dispose();
            selectedBrush?.Dispose();
            valueBrush?.Dispose();
            backgroundBrush?.Dispose();
        }
    }
} 